package com.kaishengit.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import com.kaishengit.entity.Contact;
import com.kaishengit.entity.Project;
import com.kaishengit.util.DBHelp;
import com.kaishengit.util.RowMapper;

public class ContactDao {
	private DBHelp<Contact> db=new DBHelp<Contact>();

	public void save(Contact c) {
	    final String sql="INSERT INTO t_contact(id,`name`,company,cellphone,phone,email,address,website,weibo,introduction,t_project_id,createtime)VALUES(?,?,?,?,?,?,?,?,?,?,?,?)";
		db.executeSQL(sql,c.getId(),c.getName(),c.getCompany(),c.getCellphone(),c.getPhone(),c.getEmail(),c.getAddress(),c.getWebsite(),c.getWeibo(),c.getIntroduction(),c.getProject().getId(),c.getCreatetime());
	}

	public List<Contact> findByProjectId(String projectId){
		StringBuilder sql=new StringBuilder();
		sql.append("SELECT tc.id,tc.`name`,company,cellphone,phone,email,address,website,weibo,introduction,t_project_id,tc.createtime,tp.`name` AS projectname,`desc`,tp.createtime as time FROM t_contact AS tc ");
		sql.append("LEFT JOIN t_project AS tp ON tp.id=tc.t_project_id ");
		sql.append("WHERE tc.t_project_id=? ");
		return db.executeQueryForList(sql.toString(), new ContactRowMapper(), projectId);
	}
	public Contact findById(String id){
		StringBuilder sql=new StringBuilder();
		sql.append("SELECT tc.id,tc.`name`,company,cellphone,phone,email,address,website,weibo,introduction,t_project_id,tc.createtime,tp.`name` AS projectname,`desc`,tp.createtime as time FROM t_contact AS tc ");
		sql.append("LEFT JOIN t_project AS tp ON tp.id=tc.t_project_id ");
		sql.append("WHERE tc.id=? ");
		return db.executeQueryForObject(sql.toString(), new ContactRowMapper(),id);
		
		
	}
	
	private class ContactRowMapper implements RowMapper<Contact>{

		public Contact mapperRow(ResultSet rs) throws SQLException {
			Contact c=new Contact();
			c.setId(rs.getString("id"));
			c.setCompany(rs.getString("company"));
			c.setAddress(rs.getString("address"));
			c.setCellphone(rs.getString("cellphone"));
			c.setEmail(rs.getString("email"));
			c.setName(rs.getString("name"));
			c.setPhone(rs.getString("phone"));
			c.setCreatetime(rs.getString("createtime"));
			c.setIntroduction(rs.getString("introduction"));
			c.setWebsite(rs.getString("website"));
			c.setWeibo(rs.getString("weibo"));
			
			Project p=new Project();
			p.setId(rs.getString("t_project_id"));
			p.setName(rs.getString("projectname"));
			p.setDesc(rs.getString("desc"));
			p.setCreatetime(rs.getString("time"));
			
			c.setProject(p);
			c.setProjectId(rs.getString("t_project_id"));
			
			return c;
		}
		
	}

	public void update(String id, String name, String company,String cellphone, String phone, String email, String address,
			String website, String weibo, String introduction) {
		final String sql="UPDATE t_contact SET `name`=?,company=?,cellphone=?,phone=?,email=?,address=?,website=?,weibo=?,introduction=? WHERE id=?";
		db.executeSQL(sql,name,company,cellphone,phone, email,address,website,weibo,introduction,id);
	}

	public void del(String id) {
			String sql="delete from t_contact where id=?";
			db.executeSQL(sql.toString(),id);
	}
	
	
}
